 /*
 ------------------------------------------------------------------------------
	Winmad - a test program for using mad library on Windows.

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#pragma once
#define _CRT_SECURE_NO_WARNINGS

#include "targetver.h"

#include <fcntl.h>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <mmsystem.h>

#include "mad/mad.h"
#include "mad/id3tag.h"
#include "wmm.h"

/*
 * The following utility routine performs simple rounding, clipping, and
 * scaling of MAD's high-resolution samples down to 16 bits. It does not
 * perform any dithering or noise shaping, which would be recommended to
 * obtain any exceptional audio quality. It is therefore not recommended to
 * use this routine if high-quality output is desired.
 */
inline signed int scale(mad_fixed_t sample)
{
	/* round */
	sample += (1L << (MAD_F_FRACBITS - 16));

	/* clip */
	if (sample >= MAD_F_ONE)
	{
		sample = MAD_F_ONE - 1;
	}
	else if (sample < -MAD_F_ONE)
	{
		sample = -MAD_F_ONE;
	}

	/* quantize */
	return sample >> (MAD_F_FRACBITS + 1 - 16);
}

#ifdef _DEBUG
#define TRACE(msg, ...)	{ \
							TCHAR szOutput[1024]; \
							_stprintf_s(szOutput, 1024, _T(msg), __VA_ARGS__); \
							OutputDebugString(szOutput); \
						}
#else
#define TRACE(msg, ...)
#endif
